package org.gpp.proj1.demo.menu;

import java.io.IOException;
import java.util.HashMap;

import org.fenggui.Button;
import org.fenggui.ComboBox;
import org.fenggui.composites.Window;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.io.IOStreamException;
import org.fenggui.layout.StaticLayout;
import org.fenggui.xml.theme.XMLTheme;
import org.gpp.proj1.menu.fenggui.FengJMEInputHandler;
import org.gpp.proj1.particle.ParticleCreator;

import com.jme.app.SimpleGame;
import com.jme.input.FirstPersonHandler;
import com.jme.input.MouseInput;
import com.jme.math.Vector3f;
import com.jmex.effects.particles.ParticleGeometry;

public class MenuTest extends SimpleGame implements IButtonPressedListener {
		
	private FengJMEInputHandler fengInput;
	
	private ComboBox list;
	
	private org.fenggui.Display disp;
	
	private HashMap<String, ParticleGeometry> particles;

	private Window frame;
	
	public static void main(String[] args) {		
		MenuTest game = new MenuTest();
		game.start();
	}
	
    protected void simpleUpdate() {
        fengInput.update( tpf );
    }
		
    @Override
    protected void simpleRender(){
    	disp.display();
    	disp.setDepthTestEnabled( true );
    }
    
		
	@Override
	protected void simpleInitGame() {
		rootNode.setLocalScale(0.1f);
		rootNode.updateRenderState();
		
		FirstPersonHandler handler =  (FirstPersonHandler) this.input;
		handler.getMouseLookHandler().setEnabled( false );
		
		MouseInput.get().setCursorVisible( true );
		
		particles = new HashMap<String, ParticleGeometry>();
		particles.put( "fire", ParticleCreator.createParticle( "res/particle/fire.jme" ) );
		particles.put( "flames", ParticleCreator.createParticle( "res/particle/flames.jme" ) );
		particles.put( "smoke", ParticleCreator.createParticle( "res/particle/smoke.jme" ) );
		particles.put( "muzzle", ParticleCreator.createParticle( "res/particle/muzzle.jme" ) );
		
		this.initGUI();
	}
	
	private void initGUI() {
		disp = new org.fenggui.Display(new org.fenggui.render.lwjgl.LWJGLBinding());
		
		try {
			org.fenggui.FengGUI.setTheme(new XMLTheme("data/themes/QtCurve/QtCurve.xml"));
		} catch (IOException e) {
		} catch (IOStreamException e) {
		}
		
		fengInput = new FengJMEInputHandler(disp);
		
		this.frame = new Window(false, false, false);
		disp.addWidget(frame);
		frame.setTitle("Particles");
		frame.setX(50);
		frame.setY(100);
		frame.setSize(120, 100);
		frame.setShrinkable(false);
		frame.setExpandable( false );
		frame.getAppearance().setBorder( null );
		frame.updateMinSize();		
		
		frame.getContentContainer().setLayoutManager(new StaticLayout());
		
		list = new ComboBox();
		frame.getContentContainer().addWidget(list);
		list.setSize(100, list.getMinHeight());
		list.setShrinkable(false);
		list.setX( 5 );
		list.setY( 50 );		
		list.addItem("fire");
		list.addItem("flames");
		list.addItem("smoke");
		list.addItem("muzzle");
		
		Button button = new Button();
		button.setText( "Load particle");
		button.setSize( button.getMinWidth() + 5, button.getMinHeight() + 5 );
		button.setXY( 5, 10 );
		button.addButtonPressedListener( this );
		
		frame.getContentContainer().addWidget( button );
		
		disp.layout();
	}
	
	public void buttonPressed(ButtonPressedEvent event) {
		if( event.getTrigger().getText().equals( "Load particle") ) {
			String particleName = list.getLabel().getText();
			
			rootNode.detachAllChildren();
			ParticleGeometry geometry = particles.get( particleName );
			geometry.forceRespawn();
			geometry.setLocalTranslation( new Vector3f( 0, -15, -100 ) );
			rootNode.attachChild( geometry );
			rootNode.updateRenderState();
		}
	}
}
